package com.itheima.g_param;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@WebServlet(urlPatterns = "/param")
public class ParamsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //TODO:一定要在获取请求参数之前,,设置request对象的编码方式
        request.setCharacterEncoding("utf-8");
        //获取请求携带的参数
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        System.out.println(user+" : "+password);
        //获取请求的一个参数对应的多个值
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println(Arrays.toString(hobbies));
        //获取请求携带的所有参数信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (String key : parameterMap.keySet()) {
            System.out.println(key + " : " + Arrays.toString(parameterMap.get(key)));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
